<?php

class customers{
	function getCustomersList(){
		$customerList = array();
		qdbconn();
		
		$query = "SELECT * FROM customers";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$customerList[$i]['cus_id']	= $row[0];
			$customerList[$i]['cus_firstname']= $row[1];
			$customerList[$i]['cus_lastname']= $row[2];
			$customerList[$i]['cus_address']= $row[3];
			$customerList[$i]['cus_phone']= $row[4];
			$customerList[$i]['cus_status']= $row[5];
			
			$i++;
		}
		return $customerList;
	}
	
		
	function inactivateACustomer($cus_id){
		qdbconn();		
		$query = "UPDATE customers SET cus_status = 0 where cus_id = $cus_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateACustomer($cus_id){
		qdbconn();		
		$query = "UPDATE customers SET cus_status = 1 where cus_id = $cus_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editACustomer($data){
		qdbconn();
		
		$cus_id 			= $data['cus_id'];
		$cus_firstname 		= $data['cus_firstname'];
		$cus_lastname 		= $data['cus_lastname'];
		$cus_address 		= $data['cus_address'];
		$cus_phone	 		= $data['cus_phone'];
		$cus_status	 		= $data['cus_status'];
		
		$query = "SELECT count(1) FROM customers
					WHERE  cus_phone ='$cus_phone' and cus_address='$cus_address'
					and cus_firstname='$cus_firstname' and cus_lastname='$cus_lastname'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "UPDATE customers SET cus_firstname='$cus_firstname',
							cus_lastname = '$cus_lastname', cus_address = '$cus_address',
							cus_phone = '$cus_phone' 	
							WHERE cus_id = $cus_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertACustomer($data){
		qdbconn();
		$cus_firstname = $data['cus_firstname'];
		$cus_lastname = $data['cus_lastname'];
		$cus_address = $data['cus_address'];
		$cus_phone = $data['cus_phone'];
		
		$query = "SELECT count(1) FROM customers
					WHERE   cus_phone = '$cus_phone' and cus_firstname='$cus_firstname'
						and cus_lastname='$cus_lastname' and cus_address='$cus_address'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO customers
					 (cus_firstname, cus_lastname, cus_address, cus_phone)
				values ('$cus_firstname', '$cus_lastname', '$cus_address', '$cus_phone')";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	function getCustomersInfo($cus_id){
		qdbconn();		
		$query = "SELECT * FROM customers
					WHERE cus_id = '$cus_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['cus_id'] 		= $row[0];
			$current_data['cus_firstname'] 	= $row[1];
			$current_data['cus_lastname'] 	= $row[2];
			$current_data['cus_address'] 	= $row[3];
			$current_data['cus_phone'] 		= $row[4];
			$current_data['cus_status'] 	= $row[5];
							
		}
		return $current_data;
	}
}

?>